package com.vastcom.demo.action;

import com.vastcom.demo.model.Carousel;
import com.vastcom.demo.model.Singlepage;
import com.vastcom.demo.service.ManageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("/manage")
@PreAuthorize("hasAuthority('admin')")
public class AdminAction {
    @Autowired
    private ManageService manageService;


    @PostMapping("saveSinglePage")
    @ResponseBody
    public HashMap saveSinglePage(@RequestBody Singlepage singlepage)throws Exception{
        HashMap m=new HashMap();
        try {
            manageService.saveSinglepage(singlepage);

            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }

        return m;
    }

    @PostMapping("saveCarousel")
    @ResponseBody
    public HashMap saveCarousel(@RequestBody Carousel carousel)throws Exception{
        HashMap m=new HashMap();
        try {
            manageService.saveCarousel(carousel);
            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }
        return m;
    }


    @PostMapping("deleteCarousel")
    @ResponseBody
    public HashMap deleteCarousel(@RequestBody Carousel carousel)throws Exception{
        HashMap m=new HashMap();
        try {
            manageService.deleteCarousel(carousel.getCarouselid());
            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }
        return m;
    }




    @PostMapping("getCarousel")
    @ResponseBody
    public HashMap getCarousel(@RequestBody Carousel carousel)throws Exception{
        HashMap m=new HashMap();
        Carousel temp=manageService.getCarousel(carousel.getCarouselid());
        if(temp!=null){
            m.put("msg","ok");
            m.put("obj",temp);
        }
        else {
            m.put("msg","empty");
        }
        return m;
    }







}
